<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\UsersExporter;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\MessageBag;

class UsersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->column('id', __('Id'));
        $grid->column('name', __('代理账号'));
        $grid->column('proxy_name', __('代理姓名'));
        $grid->column('nickname', __('微信昵称'));

        $grid->column('money', __('余额'));
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'default'],
        ];
        $grid->column('status','状态')->switch($states);
        $grid->column('sex', __('性别'))->display(function($sex){
            return $sex==1?'男':'女';
        });
        $grid->column('created_at', __('注册时间'));
        $grid->actions(function ($actions) {
            //$actions->disableDelete();
            $actions->disableView();
        });
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->equal('name','代理账号');
            $filter->equal('proxy_name','代理商姓名');
            $filter->like('nickname','昵称');
            $filter->between('created_at', '注册时间')->datetime();
        });
        //$grid->showExportBtn();
        $grid->exporter(new UsersExporter());
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('mobile', __('Mobile'));
        $show->field('status', __('Status'));
        $show->field('avatar', __('Avatar'));
        $show->field('nickname', __('Nickname'));
        $show->field('level', __('Level'));
        $show->field('money', __('Money'));
        $show->field('score', __('Score'));
        $show->field('sex', __('Sex'));
        $show->field('bref', __('Bref'));
        $show->field('brith', __('Brith'));
        $show->field('live_address', __('Live address'));
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableDelete();
            });;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);
        $form->radio('is_proxy','用户类型')
            ->options(User::PROXY_DESC)
            ->required();
        $form->text('name', __('代理账号'))
            ->creationRules('required|min:6|unique:users',[
                'min' => '账号最少要6位',
                'unique'=>'用户已存在'
            ])
            ->updateRules('required|min:6',[
                'min' => '账号最少要6位'
            ]);
        $form->password('password', __('密码'))->default(function ($form) {
            return $form->model()->password;
        });
        $states = [
        	'on'  => [
        	    'value' => 1,
                'text' => '正常',
                'color' => 'primary'
            ],
        	'off' => [
        	    'value' => 0,
                'text' => '禁用',
                'color' => 'default'
            ],
        ];
        $form->text('proxy_name', '代理姓名');
        $form->switch('status', __('状态'))
            ->states($states)
            ->default(1);
        $form->radio('sex','性别')->options(['1'=>'男','2'=>'女']);
        $form->select('type','充值')->options(User::TYPE);
        $form->number('opt_money','金额')->default(0);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
       /* // 在表单提交前调用
        $form->submitted(function (Form $form) {
            $id = $form->model()->id;
            $user = User::find($id);
            if (!empty($user['openid'])) {
                $error = new MessageBag([
                    'title'   => '错误操作',
                    'message' => '微信用户不能成为代理',
                ]);
                return back()->with(compact('error'));
            }
        });*/
        //保存前回调
        $form->saving(function (Form $form) {
            if ($form->password && (strlen($form->password) <= 30)) {
                $form->password = Hash::make($form->password);
            }
        });

        $form->saved(function (Form $form) {
            $id = $form->model()->id;
            $user = User::find($id);
            User::adminOptMoney($form->type, $form->opt_money, $user);
            $user->type = null;
            $user->opt_money = 0;
            $user->save();
        });
        $form->footer(function ($footer) {
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });
        return $form;
    }
}
